package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.ComplaintsDao;
import cn.ym.mall.entity.Complaints;
import cn.ym.mall.entity.Order;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 赵金明
 * 2022/8/12
 */
public class ComplaintsDaoImpl implements ComplaintsDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 新增投诉
     *
     * @param complaints 添加的投诉
     * @return 返回成功的条数
     */
    @Override
    public Integer addComplaints(Complaints complaints) throws SQLException {
        String sql = "insert into ym_complaints(tsid,sid,uid,tscontent,tsfeedback,tstime,tsstate) " +
                "values (?,?,?,?,?,?,?);";
        Object[] params = new Object[]{complaints.getTsid(),complaints.getSid(),
                complaints.getTscontent(),complaints.getTsfeedback(), complaints.getTstime(),
                complaints.getTsitsstated(),complaints.getUid() };
        return queryRunner.update(DruidUtils.getConnection(), sql, params);
    }

    /**
     * 通过投诉id查询投诉信息
     *
     * @param tsid
     * @return
     */
    @Override
    public Complaints getComplaintsByTid(Integer tsid) throws SQLException {
        String sql = "select * from ym_announcement where tsid = ?;";
        return queryRunner.query(DruidUtils.getConnection(), sql, new BeanHandler<Complaints>(Complaints.class), tsid);
    }

    /**
     * 查询所有的投诉
     *
     * @return 投诉集合
     */
    @Override
    public List<Complaints> getComplaints()throws SQLException  {
        String sql="select * from ym_complaints order by tsid desc limit ?,?;";
        List<Complaints> complaintsList = new ArrayList<Complaints>();
        try {
            complaintsList=queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Complaints>(Complaints.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return complaintsList;
    }

    /**
     * 查询投诉总数
     *
     * @return
     */
    @Override
    public long getComplaintsCount()throws SQLException  {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_complaints;";
            count = queryRunner.query(DruidUtils.getConnection(), sql, new ScalarHandler<>());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 查询所有投诉举报（分页）
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<Complaints> getPageComplaints(Integer pageNo, Integer pageSize) throws SQLException {
        String sql = "select * from ym_complaints order by tstime desc limit ?,?;";
        List<Complaints> complaintsList = new ArrayList<>();
        try {
            complaintsList = queryRunner.query(DruidUtils.getConnection(), sql, new BeanListHandler<Complaints>(Complaints.class), (pageNo - 1) * pageSize, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return complaintsList;
    }
}
